﻿using System;

namespace XEvolve.Core.Runtime.Attributes
{
    [AttributeUsage(AttributeTargets.Parameter)]
    public class ExtensionsAttribute : Attribute, IDependencyAttribute
    {
        private object Identifier { get; set; }
        private bool IgnoreIfNotFound { get; set; }

        public ExtensionsAttribute(object identifier) : this(identifier, false)
        {
        }

        public ExtensionsAttribute(object identifier, bool ignoreIfNotFound)
        {
            Identifier = identifier;
            IgnoreIfNotFound = ignoreIfNotFound;
        }

        public object ProvideValue(IExtensionRegistry registry)
        {
            try
            {
                return registry.GetExtensionPoint(Identifier).Extensions;
            }
            catch (ExtensionPointNotFoundException)
            {
                if (IgnoreIfNotFound)
                {
                    return new object[]{};
                }
                throw;
            }
        }
    }
}